package com.orcsystem.product.app;

import com.orcsystem.common.utils.PageUtils;
import com.orcsystem.common.utils.R;
import com.orcsystem.product.app.vo.AttrGroupRealationVo;
import com.orcsystem.product.entity.AttrEntity;
import com.orcsystem.product.entity.AttrGroupEntity;
import com.orcsystem.product.service.AttrAttrgroupRelationService;
import com.orcsystem.product.service.AttrGroupService;
import com.orcsystem.product.service.AttrService;
import com.orcsystem.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author aihs
 * @email aihs_work@163.com
 * @date 2020-04-29 14:16:13
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService relationService;

    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody List<AttrGroupRealationVo> attrGs){
        relationService.saveBatch(attrGs);
        return R.ok();
    }

    @GetMapping("/{attrGroupId}/noattr/relation")
    public R noattrRelation(@PathVariable("attrGroupId") Long attrGroupId , @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getNoRelationAttr(attrGroupId,params);
        return R.ok().put("page",page);
    }

    /**
     * 删除
     * @param vos
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRealationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 获取所有分组 和属性的关联
     * @return
     */
    //product/attrgroup/6/attr/relation
    @GetMapping("{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrEntity=attrService.getRelationAttr(attrgroupId);
       return R.ok().put("data",attrEntity);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		if (attrGroup == null){
		    return null;
        }
        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
